package com.ruoyi.partybuilding.domain;

import java.math.BigDecimal;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.easy.query.core.annotation.EasyWhereCondition;
import com.easy.query.core.annotation.Table;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.partybuilding.common.EQuery.ICreatorIdEntity;
import com.ruoyi.partybuilding.common.EQuery.IDeptEntity;
import lombok.Data;
import lombok.var;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import io.swagger.annotations.*;

import javax.validation.constraints.*;

/**
 * 个人积分对象 pb_work_score
 * 
 * @author he-liangliang@outlook.com
 * @date 2024-11-27
 */
@Table("pb_work_score")
@Data
public class WorkScore extends IDeptEntity.LongId implements ICreatorIdEntity
{
    private static final long serialVersionUID = 1L;

    public final  static int MaxApprovalLevel=1;

    public final  static int ApprovalPassEnd =10000;
    /** 用户id ; */
    @ApiModelProperty(value = "用户id")
    @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
    private Long userId;

    /** 党员名称 ; */
    @ApiModelProperty(value = "党员名称")
    @EasyWhereCondition
    @Excel(name = "党员名称",sort = 1)
    private String userName;

    /** 部门id ; */
    @ApiModelProperty(value = "部门id")
    @NotNull(message = "组织部门id错误")
    @Min(value = 1,message = "组织部门id错误")
    @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
    private Long deptId;

    /** 组织名称 ; */
    @ApiModelProperty(value = "组织名称")
    @NotNull(message = "组织部门名称错误")
    @Size(min = 1,max = 50,message = "组织部门名称错误")
    @EasyWhereCondition
    @Excel(name = "组织名称",sort = 2)
    private String orgName;

    /** 0个人积分，1组织积分 ; */
    @ApiModelProperty(value = "0个人积分，1组织积分")
    private String type;

    /** 类型名称 ; */
    @ApiModelProperty(value = "分类名称",notes = "积分所属分类名称")
    @NotBlank(message = "分类名称错误")
    @Size(min=1)
    //@Excel(name = "分类名称",sort = 10)
    private String typeName;

    /** 积分类型 ; */
    @ApiModelProperty(value = "分类id",notes = "积分所属分类id")
    @NotNull(message = "分类id错误")
    @Min(value = 1,message = "分类id错误")
    @EasyWhereCondition(type= EasyWhereCondition.Condition.EQUAL)
    private Long typeId;

    /** 积分题目 ; */
    @ApiModelProperty(value = "积分规则")
    @NotBlank(message = "积分规则错误")
    @Size(min = 1)
    @Excel(name = "积分规则",sort = 3)
    private String title;

    /** 题目id ; */
    @ApiModelProperty(value = "积分规则id")
    @NotNull(message = "积分规则错误")
    @Min(value = 1,message = "积分规则错误")
    private Long titleId;

    /** 周期值，月 20012 ,季度 21004,半年 22002,年 32024 ; */
    @ApiModelProperty(value = "周期，0：月度；1：季度；2：半年 3：年度",notes = "冗余规则周期")
    @NotNull
    @Min(0)
    @Max(4)
    @Excel(name = "周期",readConverterExp = "0=月度,1=季度,2=半年,3=年度",sort = 5)
    private Long cycle;

    @ApiModelProperty(value = "周期值")
    @NotNull
    @Min(1)
    @Max(2999)
    Long cycleValue;

    /** 原始积分 ; */
    @ApiModelProperty(value = "原始积分")
    private BigDecimal scoreOrigin;

    /** 申请积分 ; */
    @ApiModelProperty(value = "申请积分")
    @NotNull
    @Excel(name = "申请积分",sort = 6)
    private BigDecimal addScore;

    /** 审批状态：驳回层级，0待审核，10000审核通过 ; */
    @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
    @ApiModelProperty(value = "审批状态：0待审,10000通过,-10000驳回",notes = "0待审核，10000审核结束通过,-10000审核结束驳回")
    @Excel(name = "审批状态",readConverterExp = "0=待审核,10000=审核通过,-10000=驳回",sort = 8)
    private Long approvalStatus;

    /** 审批备注 ; */
    @ApiModelProperty(value = "审批备注")
    @Excel(name = "审批备注",sort = 9)
    private String approvalRemark;

    /** 审批记录[[时间，人员，结果,备注]] ; */
    @ApiModelProperty(value = "审批记录[{时间，人员，结果,备注}]")
    private String approvalRecord;

    @ApiModelProperty(value = "申请原因")
    @Excel(name = "申请原因",sort = 7)
    String remark;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "申请时间",dateFormat = "yyyy-MM-dd",sort = 4)
    Date createTime;
    @ApiModelProperty("创建人id")
    Long creatorId;

    @ApiModelProperty("所属年度")
    @NotNull
    @Min(value = 2000)
    @EasyWhereCondition(type = EasyWhereCondition.Condition.EQUAL)
    Long yearSign;
    public void approveSet(int unifyStatus, String unifyRemark,String approvalUserName) {

        WorkScore item=this;
        int maxLevel=MaxApprovalLevel;
        if (unifyStatus == 1) {
            var oldStatus = item.getApprovalStatus();
            var arr= JSONUtil.parseArray(item.getApprovalRecord());
            var newStatus = ++oldStatus;
            arr.add(new String[]{DateUtil.now(),approvalUserName,newStatus+"", unifyRemark}); //时间，人员，结果,备注
            item.setApprovalStatus(newStatus);
            if (newStatus >= maxLevel) {
                item.setApprovalStatus((long) WorkScore.ApprovalPassEnd);
                arr.add(new String[]{DateUtil.now(),approvalUserName,WorkScore.ApprovalPassEnd +"","审批通过完结"});
            }
            var recordStr=JSONUtil.toJsonStr(arr);
            item.setApprovalRecord(recordStr);
            item.setApprovalRemark(unifyRemark);
        }else if(unifyStatus ==-1) {
            var oldStatus = item.getApprovalStatus();
            var arr = JSONUtil.parseArray(item.getApprovalRecord());
            var newStatus = ++oldStatus;
            var newStatusVal=-newStatus;
            arr.add(new String[]{DateUtil.now(), approvalUserName, newStatusVal + "", unifyRemark}); //时间，人员，结果,备注
            item.setApprovalStatus(newStatusVal);
            if (newStatus >= maxLevel) {
                item.setApprovalStatus((long) -WorkScore.ApprovalPassEnd);
                arr.add(new String[]{DateUtil.now(),approvalUserName,-WorkScore.ApprovalPassEnd +"","审批驳回完结"});
            }
            var recordStr = JSONUtil.toJsonStr(arr);
            item.setApprovalRecord(recordStr);
            item.setApprovalRemark(unifyRemark);
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("userId", getUserId())
            .append("userName", getUserName())
            .append("deptId", getDeptId())
            .append("orgName", getOrgName())
            .append("type", getType())
            .append("typeName", getTypeName())
            .append("typeId", getTypeId())
            .append("title", getTitle())
            .append("titleId", getTitleId())
            .append("cycle", getCycle())
            .append("scoreOrigin", getScoreOrigin())
            .append("addScore", getAddScore())
            .append("remark", getRemark())
            .append("createTime", getCreateTime())
            .append("approvalStatus", getApprovalStatus())
            .append("approvalRemark", getApprovalRemark())
            .append("approvalRecord", getApprovalRecord())
            .toString();
    }
}
